package com.changcheng.controller;
import com.changcheng.pojo.Record;
import com.changcheng.service.RecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/record/*")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @RequestMapping("findAllRecord")
    public List<Record> findAllRecord(int offset, int limit){
        return recordService.findAllRecord(offset,limit);
    }
    @RequestMapping("findRecord")
    public List<Record> findRecord(@RequestParam(value = "userID",required = false) String userIDStr,
                                   @RequestParam(value ="startDate",required = false) String startDateStr,
                                   @RequestParam(value ="endDate",required = false) String endDateStr,
                                   @RequestParam("offset") int offset,
                                   @RequestParam("limit") int limit) throws ParseException {
        String regex = "\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}";
        boolean startDateFormatCheck = (StringUtils.isEmpty(startDateStr) || startDateStr.matches(regex));
        boolean endDateFormatCheck = (StringUtils.isEmpty(endDateStr) || endDateStr.matches(regex));
        boolean userIDCheck = (StringUtils.isEmpty(userIDStr) || StringUtils.isNumeric(userIDStr));
        if (startDateFormatCheck && endDateFormatCheck && userIDCheck) {
            int userId=-1;
            if (StringUtils.isNumeric(userIDStr)){
                userId=Integer.valueOf(userIDStr);
            }
            return recordService.findRecord(userId,startDateStr,endDateStr,offset,limit);
        }
        List list=new ArrayList();
        list.add("error");
        return list;
    }
}
